package com.foo.idcardidentify.service;

import com.foo.idcardidentify.dao.IIdcardDao;
import com.foo.idcardidentify.entity.IdcardEntity;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.transaction.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class IdcardService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IIdcardDao dao;
    @Autowired
    private IdentifyService identifyService;

    @Transactional
    public void save(IdcardEntity entity) {
        dao.save(entity);
    }

    @Transactional
    public void update(IdcardEntity entity) {
        dao.update(entity);
    }

    @Transactional
    public void batchSave(List<IdcardEntity> entityList) {
        if (entityList != null) {
            int size = entityList.size();
            for (int i = 0; i < size; i++) {
                dao.save(entityList.get(i));
            }
        }
    }

    public List<IdcardEntity> findAll() {
        return dao.findAll();
    }

    public IdcardEntity identify(IdcardEntity src) {
        String idCard = src.getNo();
        if (StringUtils.isEmpty(idCard)) {
            return src;
        }
        if (identifyService.validateCard(idCard)) {
            Short year = identifyService.getYearByIdCard(idCard);
            Short month = identifyService.getMonthByIdCard(idCard);
            Short day = identifyService.getDateByIdCard(idCard);
            src.setVerify(identifyService.validateCard(idCard) ? 1 : 0);
            src.setBirthDate(strYYMMDDToDate(identifyService.getBirthByIdCard(idCard)));
            src.setAge(identifyService.getAgeByIdCard(idCard));
            src.setProvince(identifyService.getProvinceByIdCard(idCard));
            String addressJson = identifyService.getCityByIdCard(idCard);
            Gson gson = new Gson();
            Map map = gson.fromJson(addressJson, Map.class);
            if (map != null) {
                Object citiyValue = map.get("citiy");
                if (citiyValue != null) {
                    src.setCity(citiyValue.toString());
                }
                Object areasValue = map.get("areas");
                if (areasValue != null) {
                    src.setArea(areasValue.toString());
                }
            }
            src.setAddress(addressJson);
            src.setBirthYear(year.intValue());
            src.setBirthMonth(month.intValue());
            src.setBirthDay(day.intValue());
            src.setGender(identifyService.getGenderByIdCard(idCard));
            try {
                String constellation = identifyService.getConstellation(month, day);
                src.setConstellation(constellation);
            } catch (Exception e) {
                logger.error("星座转换失败-month-{}-day-{}", month, day);
            }
            Calendar c = Calendar.getInstance();
            c.set(year, month, day);
            src.setZodiac(identifyService.date2Zodica(c));
        }
        return src;
    }

    public static Date strYYMMDDToDate(String dateString) {
        if (null == dateString) {
            return null;
        }
        try {
            return new SimpleDateFormat("yyyyMMdd").parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }
}
